package com.cetcs.kmga.dataManager.controller;

import com.alibaba.fastjson.JSON;
import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.BaseController;
import com.cetcs.kmga.common.BaseResponse;
import com.cetcs.kmga.common.Constant;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataManager.annotation.LogAnnotation;
import com.cetcs.kmga.dataManager.entity.dataManager.ServConfigPojo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.FlipHistogramChartVo;
import com.cetcs.kmga.dataManager.entity.global.LogLevelEnum;
import com.cetcs.kmga.dataManager.entity.global.LogTypeEnum;
import com.cetcs.kmga.dataManager.service.sysManager.ServConfigService;
import com.cetcs.kmga.dataManager.util.CheckIpUtil;
import com.cetcs.kmga.dataManager.util.NumConversion;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采集控制器服务层
 *
 * @author xutao
 * @version V1.0 创建时间：2017-11-16 15:25
 *          Copyright 2017 by CETC
 */
@RestController
@RequestMapping("/servConfig")
public class ServConfigController extends BaseController {

    @Autowired
    private ServConfigService servConfigService;

    /**
     * 按条件分页查询
     *
     * @param name     条件 ：servIp 服务IP;  servName 服务名称
     * @param page
     * @param pageSize
     * @return
     */
    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "按条件分页查询", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findServConfigByCondition", method = RequestMethod.GET)
    public BaseResponse findServConfigByCondition(@RequestParam(value = "keyType", required = false) String name,
                                                  @RequestParam(value = "keyValue", required = false) String keyword,
                                                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        try {
            Data param = new Data( );
            if (StringUtils.isNotBlank(keyword)) {
                param.put(name, keyword);
            }
            return this.ok(servConfigService.findServConfigByCondition(param, page, pageSize));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /**
     * 创建服务
     *
     * @param record
     * @return
     */
    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "创建服务", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/createServConfig", method = RequestMethod.POST)
    public BaseResponse createServConfig(@RequestBody ServConfigPojo record) {
        try {
            if (!CheckIpUtil.isIpValid(record.getServerIp( ))) {
                return this.error(Constant.SYSTEM_EXCEPTION, "服务器的IP地址输入有误");
            }
            if (!NumConversion.isPositiveInt(record.getPort( ))) {
                return this.error(Constant.SYSTEM_EXCEPTION, "端口号输入有误");
            }
            int ret = 0;
            record.setTime(DateUtils.getTime( ));
            if (StringUtils.isNotBlank(record.getId( ))) {
                ret = servConfigService.modifyServConfigById(record);
            } else {
                record.setStatus("1");
                ret = servConfigService.createServConfig(record);
            }
            if (ret == -1) {
                return this.error(Constant.SYSTEM_EXCEPTION, "服务器的IP地址不能重复");
            }
            return this.ok( );
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 根据ID查询服务的配置
     *
     * @param id
     * @return
     */
    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "根据ID查询服务的配置", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findServConfigById", method = RequestMethod.GET)
    public BaseResponse findServConfigById(@RequestParam(value = "id", required = false) String id) {
        try {
            if (!StringUtils.isNotBlank(id)) {
                return this.error(Constant.SYSTEM_EXCEPTION, "id不能为空");
            }
            return this.ok(servConfigService.findServConfigById(id));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "生成服务查询条件", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findServConfigQuery", method = RequestMethod.GET)
    public BaseResponse findServConfigQuery() {
        try {
            List<Map<String, Object>> ret = Lists.newArrayList( );
            Map<String, Object> param = new HashMap<>( );
            param.put("id", "servName");
            param.put("value", "服务名称");
            ret.add(param);
            param = new HashMap<>( );
            param.put("id", "servIp");
            param.put("value", "IP地址");
            ret.add(param);
            return this.ok(ret);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "查询服务的类型", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findServConfigType", method = RequestMethod.GET)
    public BaseResponse findServConfigType() {
        try {
            List<Map<String, Object>> ret = Lists.newArrayList( );
            Map<String, Object> param = new HashMap<>( );
            param.put("id", "1");
            param.put("value", "RabbitMQ");
            ret.add(param);
            return this.ok(ret);
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 更新服务信息
     *
     * @param record
     * @return
     */
    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "更新服务信息", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/modifyServConfigById", method = RequestMethod.POST)
    public BaseResponse modifyServConfigById(@RequestBody ServConfigPojo record) {
        try {
            int ret = servConfigService.modifyServConfigById(record);
            if (ret == -1) {
                return this.error(Constant.SYSTEM_EXCEPTION, "服务的IP地址不能重复");
            }
            return this.ok( );
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 更新服务状态
     *
     * @param record 数据
     * @return
     */
    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "更新服务状态", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/modifyServStatusById", method = RequestMethod.POST)
    public BaseResponse modifyServStatusById(@RequestBody ServConfigPojo record) {
        try {
            return this.ok(servConfigService.modifyServStatusById(record.getId( ), record.getStatus( )));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 批量删除服务
     *
     * @param record
     * @return
     */
    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "批量删除服务", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/removeServConfigByIds", method = RequestMethod.POST)
    public BaseResponse removeServConfigByIds(@RequestBody Data record) {

        try {
//            Data params = getParam(request);
//            String json = params.getString("postData");
//            List<String> ids = JSON.parseArray(json, String.class);
            List<String> ids = (List<String>) record.get("del");
            return this.ok(servConfigService.removeServConfigByIds(ids));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 删除单个服务
     *
     * @param id
     * @return
     */
    @LogAnnotation(module = "系统管理-系统信息-采集服务器", logType = LogTypeEnum.业务日志, description = "删除单个服务", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/removeServConfigById/{id}", method = RequestMethod.DELETE)
    public BaseResponse removeServConfigById(@PathVariable("id") String id) {
        try {
            return this.ok(servConfigService.removeServConfigById(id));
        } catch (Exception e) {
            e.printStackTrace( );
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


}
